package top.yangbuyi.controller.system;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.yangbuyi.acpect.annotaction.Log;
import top.yangbuyi.acpect.enums.BusinessType;
import top.yangbuyi.dto.DictDataDto;
import top.yangbuyi.service.impl.DictDataService;
import top.yangbuyi.utils.ShiroSecurityUtils;
import top.yangbuyi.vo.AjaxResult;
import top.yangbuyi.vo.DataGridView;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;


/**
 * @description: 杨不易网站:www.yangbuyi.top
 * @program: yangbuyiopenHis2020
 * @ClassName: DictDataController
 * @create: 2020-08-29 23:28
 * @author: yangbuyi
 * @since： JDK1.8
 * @DictDataController: 字典管理---数据字典$
 **/

@RestController
@Slf4j
@RequestMapping("system/dict/data")
public class DictDataController {
	  @Autowired
	  private DictDataService dictDataService;

	  /**
	   * 分页查询
	   */
	  @GetMapping("listForPage")
	  @Log(title = "分页查询字典数据", businessType = BusinessType.DELETE)
	  public AjaxResult listForPage(DictDataDto dictDataDto) {
			DataGridView gridView = this.dictDataService.listPage(dictDataDto);
			return AjaxResult.success("查询成功", gridView.getData(), gridView.getTotal());
	  }

	  /**
	   * 添加
	   */
	  @PostMapping("addDictData")
	  @Log(title = "添加字典数据", businessType = BusinessType.INSERT)
	  public AjaxResult addDictData(@Validated DictDataDto dictDataDto) {
			dictDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
			return AjaxResult.toAjax(this.dictDataService.insert(dictDataDto));
	  }

	  /**
	   * 修改
	   */
	  @PutMapping("updateDictData")
	  @Log(title = "修改字典数据", businessType = BusinessType.UPDATE)
	  public AjaxResult updateDictData(@Validated DictDataDto dictDataDto) {
			dictDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
			return AjaxResult.toAjax(this.dictDataService.update(dictDataDto));
	  }


	  /**
	   * 根据ID查询一个字典信息
	   */
	  @GetMapping("getOne/{dictCode}")
	  @Log(title = "根据ID查询字典数据", businessType = BusinessType.SELECT)
	  public AjaxResult getDictData(@PathVariable @Validated @NotNull(message = "数据字典ID不能为空") Long dictCode) {
			return AjaxResult.success(this.dictDataService.selectDictDataById(dictCode));
	  }

	  /**
	   * 删除
	   */
	  @DeleteMapping("deleteDictDataByIds/{dictCodeIds}")
	  @Log(title = "根据ID数组批量删除字典数据", businessType = BusinessType.DELETE)
	  public AjaxResult deleteDictData(@PathVariable @Validated @NotEmpty(message = "要删除的数据字典ID不能为空") Long[] dictCodeIds) {
			return AjaxResult.toAjax(this.dictDataService.deleteDictDataByIds(dictCodeIds));
	  }

	  /**
	   * 根据字典类型查询字典数据
	   */
	  @GetMapping("getDataByType/{dictType}")
	  public AjaxResult getDataByType(@PathVariable @Validated @NotEmpty(message = "数据字典类型不能为空") String dictType) {
			return AjaxResult.success(this.dictDataService.selectDictDataByDictType(dictType));
	  }


}
